﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Visifire.Charts;

namespace AutoMeasureV2._1
{
    /// <summary>
    /// chartUC.xaml 的交互逻辑
    /// </summary>
    public partial class chartUC : UserControl
    {
        public chartUC()
        {
            InitializeComponent();
            ChartParameters(); 
        }

        private double xMaxtime = 0;  //最大时间长度
        private double xMaxValue = 0;  //最大Y值

        DataPoint timePoint = new DataPoint();
        DataSeries timeSeries = new DataSeries();


        private void ChartParameters()
        {
            root.Children.Clear();

            //创建一个图例
            Chart chart = new Chart();
            
            chart.BorderThickness = new Thickness(1);
            chart.ScrollingEnabled = false;

            Axis yAxis = new Axis();
            yAxis.AxisMinimum = 0;
            yAxis.Interval = 10;
            yAxis.AxisMaximum = 200;
            chart.AxesY.Add(yAxis );
            chart.Background = Brushes.FloralWhite;
            chart.Theme = "Theme5";
            Title title = new Title();

            title.Text = "自动化执行工谱图";
            title.FontSize = 16;
            title.Padding = new Thickness(0,2,2,0);

            chart.Titles.Add(title);
            root.Children.Add(chart);
        }

        private fileHandle handle;
        public fileHandle Handle
        {
            set
            {
                root.Children.Clear();
                handle = value;
                Chart chart = new Chart();
                chart.ZoomingEnabled = true;

                xMaxtime = 0;
                xMaxValue = 0;

                Title title = new Title();
                title.Text = "自动化执行工谱图";
                title.FontSize = 14;
                title.Padding = new Thickness(0, 5, 5, 0);

                chart.Titles.Add(title);
                chart.Theme = "Theme5";
                chart.Background = Brushes.FloralWhite;

                foreach (var source in value.source)
                {
                    DataSeries dataSeries = new DataSeries();
                    dataSeries.Name = source.UseName;
                    double xTime = 0;
                    double xValue = 0;

                    int digtType = Convert.ToInt16(source .ObjDigType );
                    if (digtType == 0)  //数据类型：模拟量
                    {
                       dataSeries.RenderAs = RenderAs.Line;
                        dataSeries.AxisYType = AxisTypes.Primary ;
                        for (int i = 0; i < source.StepCount; i++)
                        {
                            xTime = source.Steps[i].OrderTime + source.Steps[i].ChangeTime;
                            xValue = source.Steps[i].TargetValue;
                            if (xValue >= xMaxValue)
                                xMaxValue = xValue;
                            if (xTime >= xMaxtime)
                                xMaxtime = xTime + 20;
                            if (i == 0)
                            {
                                DataPoint dataPoint = new DataPoint();
                                dataPoint.XValue = 0;
                                dataPoint.YValue = 0;
                                dataSeries.DataPoints.Add(dataPoint);
                                DataPoint dataPoint1 = new DataPoint();
                                dataPoint1.XValue = source.Steps[i].OrderTime + source.Steps[i].ChangeTime;
                                dataPoint1.YValue = source.Steps[i].TargetValue;
                                dataSeries.DataPoints.Add(dataPoint1);
                            }
                            else
                            {
                                DataPoint dataPoint = new DataPoint();
                                dataPoint.XValue = source.Steps[i].OrderTime;
                                dataPoint.YValue = source.Steps[i - 1].TargetValue;
                                dataSeries.DataPoints.Add(dataPoint);
                                DataPoint dataPoint1 = new DataPoint();
                                dataPoint1.XValue = source.Steps[i].OrderTime + source.Steps[i].ChangeTime;
                                dataPoint1.YValue = source.Steps[i].TargetValue;
                                dataSeries.DataPoints.Add(dataPoint1);
                            }
                        }
                    }
                    else if (digtType == 1)  //数据类型：数字量
                    {
                        dataSeries.RenderAs = RenderAs.StepLine;
                        dataSeries.AxisYType = AxisTypes.Secondary;
                        DataPoint dataPoint = new DataPoint();
                        dataPoint.XValue = 0;
                        dataPoint.YValue = 0;
                        dataSeries.DataPoints.Add(dataPoint);
                        foreach (var step in source.Steps)
                        {
                            DataPoint dataPoint1 = new DataPoint();
                            xTime = step.OrderTime + step.ChangeTime;
                            xValue = step.TargetValue;
                            if (xValue >= xMaxValue)
                                xMaxValue = xValue;
                            if (xTime >= xMaxtime)
                                xMaxtime = xTime + 20;

                            dataPoint1.XValue = step.OrderTime;
                            dataPoint1.YValue = step.TargetValue;
                            dataSeries.DataPoints.Add(dataPoint1);
                        }
                    }
                    else if (digtType == 2)  //数据类型：开关量
                    {
                        dataSeries.RenderAs = RenderAs.Point;
                        dataSeries.AxisYType = AxisTypes.Secondary;
                        dataSeries.MarkerScale = 1;
                        foreach (var step in source.Steps)
                        {
                            DataPoint dataPoint = new DataPoint();
                            xTime = step.OrderTime + step.ChangeTime;
                            xValue = step.TargetValue;
                            if (xValue >= xMaxValue)
                                xMaxValue = xValue;
                            if (xTime >= xMaxtime)
                                xMaxtime = xTime + 20;
                            dataPoint.XValue = step.OrderTime;
                            dataPoint.YValue = step.TargetValue;
                            dataSeries.DataPoints.Add(dataPoint);
                        }
                    }
                    else
                    {
                        MessageBox.Show("数据类型异常");
                        break;
                    }

                    chart.Series.Add(dataSeries);
                }
                Axis xAxis = new Axis();
                xAxis.AxisMaximum = xMaxtime + 10;
                chart.AxesX.Add(xAxis);

                timePoint.XValue = 0;
                timePoint.YValue = xMaxValue;
            
                chart.DataPointWidth = 0.25;
                root.Children.Add(chart);
            }
        }


    }
}
